package com.stone.business.plan.action;

/**
 * -----计划  ACTION---
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.gzrwd.pojo.TGzrwdRw;
import com.stone.business.plan.pojo.TBzgzrw;
import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.pojo.TPlanBz;
import com.stone.business.plan.service.PlanService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBdz;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.UserService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;
import com.stone.utils.MainUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;

public class PlanAction extends BaseAction {
	private PlanService planService;
	private TPlan entity;

	private String id;
	private String status;
	private List<TBdz> bdzList;
	private List<TDictionary> dydjList;

	private List<TDictionary> sblxList;
	private List<TPlanBz> planbzList;

	private String hiddenids;
	private List list;
	private String datafrom;
	private String showtype;
	//班组
	private List<TOrgan> bzList;
		public List<TOrgan> getBzList() {
		return bzList;
	}

	public void setBzList(List<TOrgan> bzList) {
		this.bzList = bzList;
	}
	/**
	 * 进入月度工作计划详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initMonthPlanDetail() throws AppException {

		// 修改进入
		if (!"0".equals(id.toString())) {

			this.entity = (TPlan) planService.findById(Long.parseLong(id),
					TPlan.class.getName());

			// 新建进入
		} else {

			this.entity = new TPlan();
			String sysdate = planService.getSysdate("yyyy-MM-dd");
			this.entity.setStartdate(DateFormatTool.formatToDate(sysdate
					+ " 09:00", true));
			this.entity.setEnddate(DateFormatTool.formatToDate(sysdate
					+ " 18:00", true));
			this.entity.setStatus("0");
			this.entity.setPlantype(super.getSearchBean().getCon6());
			this.entity.setOuid(super.getSession().getAttribute(
					IConstant.SESSION_TORGAN_CODE).toString());
			entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			
		}

		setDictionary();

		return SUCCESS;

	}
	/**
	 * 由遗留问题进入月度工作计划详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initYlwtMonthPlanDetail() throws AppException {
            TGzrwdRw rw=(TGzrwdRw) planService.findById(Long.parseLong(id),
            		TGzrwdRw.class.getName());
			TPlan tplan = (TPlan) planService.findById(rw.getPlanid(),
					TPlan.class.getName());
			this.entity = new TPlan();
			// 将旧计划复制给新计划
			MainUtils.copy(tplan, this.entity);
			this.entity.setId(Long.parseLong("0"));
			this.entity.setGzrwdrwid(rw.getId());//遗留问题的工作任务单任务id工作记录回填时候用
			String sysdate = planService.getSysdate("yyyy-MM-dd");
			this.entity.setStartdate(DateFormatTool.formatToDate(sysdate
					+ " 09:00", true));
			this.entity.setEnddate(DateFormatTool.formatToDate(sysdate
					+ " 18:00", true));
			this.entity.setStatus("0");
			this.entity.setPlantype("1");
			this.entity.setOuid(super.getSession().getAttribute(
					IConstant.SESSION_TORGAN_CODE).toString());
			entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());	
		    setDictionary();
		    List<TBzgzrw> listrw=new ArrayList<TBzgzrw>();
		    TBzgzrw bzrw = new TBzgzrw();
			// 将工作任务单任务复制给班组工作任务
			MainUtils.copy(rw, bzrw);
			bzrw.setId(0L);
		    listrw.add(bzrw);
		    planService.doSavePlan(this.entity, planbzList, listrw);


		return SUCCESS;

	}
	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String saveMonthPlan() throws AppException {
		String sysdate = planService.getSysdate("YYYY-MM-DD");
		Date confDate = DateFormatTool.formatToDate(planService
				.getSysdate("yyyy-MM-dd HH24:mi"), true);
		if (this.entity.getId() == 0) {
			this.entity.setOuid(super.getSession().getAttribute(
					IConstant.SESSION_TORGAN_CODE).toString());
			entity.setCjsj(DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd HH24:mi"),true));
			/*
			 * String strStartDate =
			 * super.getRequest().getParameter("entity.startdate"); //
			 * 设置隔日和第一日的查询条件日期 StringBuffer sql = new StringBuffer();
			 * sql.append("select to_char(TO_DATE('" + strStartDate +
			 * "', 'YYYY-MM-DD'), 'Day') as gr,");
			 * sql.append(" TO_CHAR((TO_DATE('" + strStartDate +
			 * "', 'YYYY-MM-DD') - 2),'YYYY-MM-DD') as zcgr,");
			 * sql.append(" TO_CHAR((TO_DATE('" + strStartDate +
			 * "', 'YYYY-MM-DD') - 4),'YYYY-MM-DD') as tsgr,");
			 * sql.append(" TO_CHAR((TO_DATE('" + strStartDate +
			 * "', 'YYYY-MM-DD') - 1),'YYYY-MM-DD') as zcdyr,");
			 * sql.append(" TO_CHAR((TO_DATE('" + strStartDate +
			 * "', 'YYYY-MM-DD') - 3),'YYYY-MM-DD') as tsdyr");
			 * sql.append(" from dual");
			 * 
			 * List<HashMap<String, Object>> gr =
			 * planService.findListBySql(sql.toString());
			 * 
			 * String xqj = gr.get(0).get("gr").toString() ; String zcgr =
			 * gr.get(0).get("zcgr").toString().compareTo(sysdate) <=0 ? sysdate
			 * :gr.get(0).get("zcgr").toString(); String tsgr =
			 * gr.get(0).get("tsgr").toString().compareTo(sysdate) <=0 ? sysdate
			 * :gr.get(0).get("tsgr").toString(); String zcdyr =
			 * gr.get(0).get("zcdyr").toString().compareTo(sysdate) <=0 ?
			 * sysdate :gr.get(0).get("zcdyr").toString(); String tsdyr =
			 * gr.get(0).get("tsdyr").toString().compareTo(sysdate) <=0 ?
			 * sysdate :gr.get(0).get("tsdyr").toString();
			 * 
			 * // 开始日期是周一隔日和第二日再往前推两天 if ("星期一".equals(xqj)) {
			 * 
			 * entity.setGr(DateFormatTool.formatToDate(tsgr, false));
			 * entity.setDyr(DateFormatTool.formatToDate(tsdyr, false));
			 * 
			 * // 开始日期是周二隔日再往前推两天 } else if ("星期二".equals(xqj)) {
			 * entity.setGr(DateFormatTool.formatToDate(tsgr, false)); } else {
			 * entity.setGr(DateFormatTool.formatToDate(zcgr, false));
			 * entity.setDyr(DateFormatTool.formatToDate(zcdyr, false)); }
			 */
		}
		String flag = super.getRequest().getParameter("flag");
		if (flag != null) {
			entity.setStatus(flag);
		}
		String rtn = planService.doSave(this.entity, this.planbzList, getHiddenids());

		setDictionary();

		super.setInfo(rtn);

		return SUCCESS;

	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String deleteMonthPlan() throws AppException {

		planService.doDelAll(id, TPlan.class.getName());
		planService.deleteByDHql("planid in (" + id + ")", null, TPlanBz.class
				.getName());

		return "DELETE_SUCCESS";
	}

	/**
	 * 提交 直接审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String confirmMonthPlan() throws AppException {
		planService.updateByDHql("status = '"
				+ super.getRequest().getParameter("flag") + "'", "id in ("
				+ this.id + ")", null, null, TPlan.class.getName());
		return "DELETE_SUCCESS";
	}

	/**
	 * 获得字典项目
	 */
	private void setDictionary() {

		// 变电站
		this.bdzList = planService.findListByHql(" from TBdz order by sortid");
		/*-----------add by fanghf 2012.3.15变电站下拉列表显示由电压等级和变电站名称拼接起来begin-----------*/
		if (this.bdzList != null && this.bdzList.size() > 0) {
			for (int i = 0; i < this.bdzList.size(); i++) {
				TBdz tbdz = this.bdzList.get(i);
				if (tbdz != null) {
					tbdz.setBdzname(tbdz.getDydj() + "kV" + tbdz.getBdzname());
				}
			}
		}
		/*-----------add by fanghf 2012.3.15变电站下拉列表显示由电压等级和变电站名称拼接起来begin-----------*/
		// 电压等级
		this.dydjList = planService
				.findListByHql(" from TDictionary where itemType = 'DYDJ' order by sortid");
		// 电压等级
		this.sblxList = planService
				.findListByHql(" from TDictionary where itemType = 'SBLX' order by sortid");
		String roles = (String) super.getSession().getAttribute(
				IConstant.SESSION_ROLES);
		TOrgan o = (TOrgan) super.getSession().getAttribute(
				IConstant.SESSION_TORGAN);
		String where = (roles.contains("gqgl")
				|| o.getOrganCode().toString().length() <= 6 || "1"
				.equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? ""
				: " and organCode = '" + o.getOrganCode().toString() + "'";

		// 班组
		List<TOrgan> list = planService
				.findListByHql(" from TOrgan where organNameEn like '%B' "
						+ where + " order by id");

		if ("0".equals(id)) {

			this.planbzList = new ArrayList<TPlanBz>();
			int i = 0;
			for (TOrgan organ : list) {
				TPlanBz bz = new TPlanBz();
				bz.setId(Long.parseLong("0"));
				bz.setDelflag("0");
				bz.setBzid(organ.getId());
				bz.setBzname(organ.getOrganName());
				this.planbzList.add(bz);
				i++;
			}
		} else {
			// where = (o.getOrganCode().toString().length() <= 6 ||
			// "1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ?
			// "" : " and bzid  = '" + o.getId().toString() + "'";
			// this.planbzList =
			// planService.findListByHql(" from TPlanBz where planid = '" +
			// entity.getId() + "'" + where + " order by delflag");
			where = (roles.contains("gqgl")
					|| o.getOrganCode().toString().length() <= 6 || "1"
					.equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? ""
					: " and bzid  = '" + o.getId().toString() + "'";
			if(this.entity.getId()==0){//遗留问题过来
	            TGzrwdRw rw=(TGzrwdRw) planService.findById(Long.parseLong(id),
	            		TGzrwdRw.class.getName());
	            List<TPlanBz> planbzoldList = new ArrayList<TPlanBz>();
	            this.planbzList = new ArrayList<TPlanBz>();
				planbzoldList = planService
				.findListByHql(" from TPlanBz where planid = '"
						+ rw.getPlanid() + "'" + where + " order by id");
				for (TPlanBz bz : planbzoldList) {
					TPlanBz newbz = new TPlanBz();
					// 将旧班组复制给新班组
					MainUtils.copy(bz,newbz);
					newbz.setId(0L);
					this.planbzList.add(newbz);
				}
			}else{
			this.planbzList = planService
					.findListByHql(" from TPlanBz where planid = '"
							+ entity.getId() + "'" + where + " order by id");
			}
			if (roles.contains("gqgl")
					|| o.getOrganCode().toString().length() <= 6
					|| "1".equals(super.getSession().getAttribute(
							"SESSION_ISADMIN"))) {
				if (planbzList.size() == 1) {
					TPlanBz tbz = planbzList.get(0);
					// 未分配任务班组
					List<TOrgan> qtlist = planService
							.findListByHql(" from TOrgan where organNameEn like '%B' and id!="
									+ tbz.getBzid() + " order by id");
					for (TOrgan organ : qtlist) {
						TPlanBz bz = new TPlanBz();
						bz.setId(Long.parseLong("0"));
						bz.setDelflag("0");
						bz.setBzid(organ.getId());
						bz.setBzname(organ.getOrganName());
						this.planbzList.add(bz);

					}
				}
			}else if(roles.contains("ZBZ")
					||roles.contains("bzgl")
					) {
				if (planbzList.size() == 1) {
					TPlanBz tbz = planbzList.get(0);
					// 未分配任务班组并且是当前用户班组
					List<TOrgan> qtlist = planService
							.findListByHql(" from TOrgan where organNameEn like '%B' and id!="
									+ tbz.getBzid()+" and id="+o.getId() + " order by id");
					for (TOrgan organ : qtlist) {
						TPlanBz bz = new TPlanBz();
						bz.setId(Long.parseLong("0"));
						bz.setDelflag("0");
						bz.setBzid(organ.getId());
						bz.setBzname(organ.getOrganName());
						this.planbzList.add(bz);

					}
				}else if (planbzList.size() == 0) {
					
					// 未分配任务班组并且是当前用户班组
					List<TOrgan> qtlist = planService
							.findListByHql(" from TOrgan where organNameEn like '%B' and id="+o.getId() + " order by id");
					for (TOrgan organ : qtlist) {
						TPlanBz bz = new TPlanBz();
						bz.setId(Long.parseLong("0"));
						bz.setDelflag("0");
						bz.setBzid(organ.getId());
						bz.setBzname(organ.getOrganName());
						this.planbzList.add(bz);

					}
				}
			}

		}

	}

	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {

		String selectpage = super.getSearchBean().getCon1();

		// 修改进入
		if (!"0".equals(id)) {

			this.entity = (TPlan) planService.findById(Long.parseLong(id),
					TPlan.class.getName());
			return SUCCESS;

			// 新建进入
		} else {

			this.entity = new TPlan();
			return SUCCESS;

		}
	}

	/**
	 * 保存、审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {

		String selectpage = super.getSearchBean().getCon1();

		Date confDate = DateFormatTool.formatToDate(planService
				.getSysdate("yyyy-MM-dd HH24:mi"), true);
		if (this.entity.getId() == 0) {

			this.entity.setOuid(super.getSession().getAttribute(
					IConstant.SESSION_TORGAN_CODE).toString());
		}
		String rtn = planService.doSave(this.entity, this.planbzList,
				getHiddenids());

		super.setInfo(rtn);

		return SUCCESS;

	}

	/**
	 * -----提交计划----
	 * 
	 * @return
	 * @throws AppException
	 */
	public String submit() throws AppException {

		Date confDate = DateFormatTool.formatToDate(planService
				.getSysdate("yyyy-MM-dd HH24:mi"), true);
		this.entity = (TPlan) planService.findById(Long.parseLong(id),
				TPlan.class.getName());

		String rtn = planService.doSubmit(this.entity, status);
		super.setInfo(rtn);
		if (status == "1") {
			return "SUBMIT_SUCCESS";

		} else {

			return "PASS_SUCCESS";
		}
	}

	/**
	 * 班组
	 * 
	 * @return
	 * @throws AppException
	 */
	/*
	 * public void getBzRw() throws IOException, AppException {
	 * 
	 * //TPlanBz planBz = (TPlanBz) planService.findById(Long.parseLong(id),
	 * TPlanBz.class.getName()); // 计划班组 this.planbzList =
	 * planService.findListByHql
	 * (" from TPlanBz where delflag='0' and planid = '" + id +
	 * "' order by delflag");
	 * 
	 * JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
	 * "yyyy-MM-dd");
	 * 
	 * JSON json = JSONSerializer.toJSON(planbzList, jsonConfig);
	 * 
	 * super.getResponse().getWriter().print(json.toString()); }
	 */
	/*
	 * public void getBzRw() throws IOException, AppException {
	 * 
	 * List<Object> valsList = new ArrayList<Object>();
	 * 
	 * HashMap<String, Object> m = new HashMap<String, Object>();
	 * 
	 * // 状态 String conditions = "bz.planid = rwd.planid"; // conditions +=
	 * " and delflag='0' AND bz.planid = " + id;
	 * 
	 * m.put("cols",
	 * "bz.bzname as bzname,bz.fzrname as fzrname, bz.cyname as cyname, bz.gzrw as gzrw, bz.lsygrs as lsygrs,rwd.cxch as cxch,rwd.tcbz as tcbz"
	 * ); m.put("alias", " bz, TGzrwd rwd"); m.put("conditions",conditions);
	 * m.put("clause", " order by bz.delflag");
	 * 
	 * List<HashMap<String, Object>> list =planService.getListByDHql(
	 * "bz.bzname as bzname,bz.fzrname as fzrname, bz.bzname as bdzname, bz.cyname as cyname, bz.gzrw as gzrw, bz.lsygrs as lsygrs,rwd.cxch as cxch,rwd.tcbz as tcbz"
	 * , conditions , valsList.toArray(), "order by bz.delflag",
	 * "TPlanBz bz, TGzrwd rwd");
	 * 
	 * JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
	 * "yyyy-MM-dd"); JSON json = JSONSerializer.toJSON(list, jsonConfig);
	 * super.getResponse().getWriter().print(json.toString());
	 * 
	 * }
	 */
	public void getBzRw() throws IOException, AppException {

		List<Object> valsList = new ArrayList<Object>();
		HashMap<String, Object> m = new HashMap<String, Object>();
		/*
		 * String sql =
		 * "select bz.bzname as bzname,bz.fzrname as fzrname, bz.cyname as cyname, bz.gzrw as gzrw, bz.lsygrs as lsygrs,nvl(rwd.cxch,' ') as cxch,nvl(rwd.tcbz,' ') as tcbz,"
		 * + " case when bz.id is null then '0' else '1' end as ishavegzd" +
		 * " from tab_plan_bz bz left join tab_gzrwd rwd" +
		 * " on bz.planid = rwd.planid and bz.bzid=rwd.bgzr where bz.delflag='0'   AND bz.planid = "
		 * + id;
		 */
		// by wangtao update20120428
		
		// 查询工作任务单
		String sql = "select o.organ_name as bzname, rwd.id as id , '' as gzrw, nvl(rwd.zbzname,' ') as fzrname, nvl(rwd.zbyname,' ') as cyname, nvl(rwd.wygs,0) as lsygrs, nvl(rwd.bh,' ') as bh, nvl(rwd.rq,sysdate) as rq,nvl(rwd.cxch,' ') as cxch,nvl(rwd.tcbz,' ') as tcbz"
			+ " from tab_gzrwd rwd, sys_organ o"
			+ " where rwd.BGZR = o.id and rwd.planid = '" + id + "'"
			+ " order by rwd.rq";
		List<HashMap<String, Object>> l1 = planService.findListBySql(sql);
		
		// 查询工作任务单
		sql = "select rwd.id as id, nvl(rw.gznr,' ') as gzrw"
				+ " from tab_gzrwd rwd,tab_gzrwd_rw rw"
				+ " where rwd.planid = '" + id + "' and rw.gzrwdid = rwd.id"
				+ " order by rwd.id";
		List<HashMap<String, Object>> l2 = planService.findListBySql(sql);
		
		// 工作任务单任务放入工作任务单
		for (HashMap<String, Object> map1 : l1) {
			for (HashMap<String, Object> map2 : l2) {
				if (map2.get("id") != null && map2.get("id").toString().equals(map1.get("id").toString())) {
					String temp = map1.get("gzrw") == null ? "" : map1.get("gzrw").toString();
					String temp2 = map2.get("gzrw") == null ? "" : (map2.get("gzrw").toString() + "</br>");
					map1.put("gzrw", temp + temp2 );
				}
			}
		}

		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");
		JSON json = JSONSerializer.toJSON(l1, jsonConfig);
		super.getResponse().getWriter().print(json.toString());

	}

	// 进入工作准备详细add by fanghf 2012.3.17
	public String initTabDetail() {
		return "TAB_SUCCESS";
	}

	//
	public String getGzapList() {
		String sql = "";
		String conditions = " where (status = '9' or status = '1') ";//by wangtao update20120516待审核计划也放到安排里
		// 月度计划
		if ("2".equals(datafrom) || "1".equals(datafrom)) {
			
			  sql =
			  "select distinct(a.id),a.stationname,a.responsible,a.teamnames,TO_CHAR(a.startdate,'YYYY-MM-DD HH24:MI') as startdate,TO_CHAR(a.enddate,'YYYY-MM-DD HH24:MI') as enddate,a.powerrange,a.voltagelevel,a.workcontent,a.ysrname as ysrname,a.dwryname as dwryname,a.status,"
			 + " case when b.id is null then '0' else '1' end as ishavegzd" +
			 " from tab_plan a left join tab_gzrwd b" + " on b.planid = a.id";
			 
			// by wangtao update20120428
			//sql = "select a.id,a.stationname,a.responsible,a.teamnames,TO_CHAR(a.startdate,'YYYY-MM-DD HH24:MI') as startdate,TO_CHAR(a.enddate,'YYYY-MM-DD HH24:MI') as enddate,a.powerrange,a.voltagelevel,a.workcontent,a.ysrname as ysrname,a.dwryname as dwryname,a.status from tab_plan a";

			// 临时安排
		} else if ("3".equals(datafrom)) {
			sql = "select a.id,a.bzname as teamnames,a.startdate,a.enddate,a.tdfw as powerrange,a.dydj as voltagelevel,a.gzxm as workcontent,a.status,"
					+ " case when b.id is null then '0' else '1' end as ishavegzd"
					+ " from tab_lsap a left join tab_gzrwd b"
					+ " on b.planid = a.id";
		}

		Date sysDate = DateFormatTool.formatToDate(planService
				.getSysdate("yyyy-MM-dd"), false);
		Date grDate = DateFormatTool.backDayandWeek(sysDate, 2, false);
		Date drrDate = DateFormatTool.backDayandWeek(sysDate, 1, false);
		String gr = DateFormatTool.format(grDate, false);
		String drr = DateFormatTool.format(drrDate, false);

		if ("3".equals(super.getSearchBean().getCon20())) {
			// conditions += " and a.startdate - trunc(sysdate) = 4";
			// conditions +=
			// " AND TO_CHAR(a.startdate,'yyyy-MM-dd') = '"+gr+"'";
			/*conditions += " and '"
					+ gr
					+ "' between to_char(a.startdate,'YYYY-MM-DD') and to_char(a.enddate,'YYYY-MM-DD')";*/
			int n=DateFormatTool.dayOfWeek(sysDate, false);
			if(n==4||n==5){
				 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+2, 'yyyy-mm-dd')";
					
			}else if(n==6){
				 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+1, 'yyyy-mm-dd')";
					
			}else{
        conditions += " and '"+gr+"' between to_char(a.startdate,'YYYY-MM-DD') and to_char(a.enddate,'YYYY-MM-DD')";
			}
		} else if ("4".equals(super.getSearchBean().getCon20())) {
			// conditions += " and a.startdate - trunc(sysdate) = 1";
			// conditions +=
			// " AND TO_CHAR(a.startdate,'yyyy-MM-dd') = '"+drr+"'";
			conditions += " and '"
					+ drr
					+ "' between to_char(a.startdate,'YYYY-MM-DD') and to_char(a.enddate,'YYYY-MM-DD')";
		} else if ("5".equals(super.getSearchBean().getCon20())) {
			// conditions += " and a.startdate - trunc(sysdate) = 0";
			// conditions +=
			// " and to_char(startdate,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";
			conditions += " and to_char(sysdate,'YYYYMMDD') between to_char(a.startdate,'YYYYMMDD') and to_char(a.enddate,'YYYYMMDD')";

		}

		TOrgan o = (TOrgan) super.getSession().getAttribute(
				IConstant.SESSION_TORGAN);
		/*
		 * if (o.getOrganCode().toString().length() > 6 &&
		 * !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
		 * conditions += " and a.ouid = '" + o.getOrganCode() + "'"; }
		 */
		// 临时生产工作安排
		if ("1".equals(datafrom)) {

			conditions += " and a.plantype = '1'";
		}
		// 临时生产工作安排
		if ("2".equals(datafrom)) {
			// conditions += " and a.state = '0'";
			conditions += " and a.plantype = '2'";
			// 临时管理工作安排
		} else if ("3".equals(datafrom)) {
			conditions += " and a.state = '1'";
		}

		if ("1".equals(showtype) || "4".equals(showtype)
				|| "7".equals(showtype)) {
			 conditions += " and b.id is not null";
		} else if ("2".equals(showtype) || "5".equals(showtype)
				|| "8".equals(showtype)) {
			 conditions += " and b.id is null";
		}

		sql += conditions;
		list = planService.findListBySql(sql);
		//班组
	 	String bzsql = "from TOrgan where id <=24 and id >=21 order by id";
	 	bzList = planService.findListByHql(bzsql);
		return "GET_LIST_SUCCESS";
	}

	// 月度工作计划中的巡视日期下拉框，ajax获得
	public void getXSRQ() throws IOException {
		String sql = "select t.xsrq as itemId,t.xsrq as itemName from TAB_NEXTMONTH_XSJH t where to_char(t.xsrq,'yyyy-mm-dd')>=to_char(sysdate,'yyyy-mm-dd') and t.bdzname like '%"
				+ this.entity.getStationname() + "%'";
		// List<HashMap<String, Object>> dicList=planService.findListBySql(sql);
		UserService userServiceDIMIS = (UserService) SpringUtil.getInstance()
				.getBean("userServiceDIMIS");
		List<HashMap<String, Object>> dicList = userServiceDIMIS
				.findListBySql(sql);
		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");
		JSON json = JSONSerializer.toJSON(dicList, jsonConfig);
		super.getResponse().getWriter().print(json.toString());
	}

	/**
	 * 合并前验证
	 * 
	 * @throws Exception
	 */
	public void initPermitCheck() throws Exception {
		long count = planService
				.getTotalBySql("select count(*) as count from TAB_PLAN where id in ("
						+ getHiddenids() + ") group by stationid");
		super.getResponse().getWriter().print(count);

	}

	/**
	 * 进入合并的详细页面
	 * 
	 * @throws Exception
	 */
	public String initPermit() throws Exception {

		this.entity = new TPlan();
		
		// 变电站
		List<TPlan> planList = planService.findListByHql("from TPlan where id in ("+ getHiddenids() + ")");	
		if (planList.size() > 0) {
			entity.setStationid(planList.get(0).getStationid());
			entity.setStationname(planList.get(0).getStationname());
		}
		
		// 获取最小的开始时间和最大的结束时间
		List<HashMap<String, Object>> l = planService.getListByDHql(
				"min(startdate) as startdate, max(enddate) as enddate",
				"id in(" + this.getHiddenids() + ")", null, "", "TPlan");
		
		// 获取时间放入entity 如果没有放系统日期
		if (l.size() > 0) {
			this.entity.setStartdate((Date) l.get(0).get("startdate"));
			this.entity.setEnddate((Date) l.get(0).get("enddate"));
		} else {
			String sysdate = planService.getSysdate("yyyy-MM-dd");
			this.entity.setStartdate(DateFormatTool.formatToDate(sysdate
					+ " 09:00", true));
			this.entity.setEnddate(DateFormatTool.formatToDate(sysdate
					+ " 18:00", true));
		}

		this.entity.setStatus("0");
		this.entity.setPlantype(super.getSearchBean().getCon6());
		this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		
		// 字典及班组
		setDictionary();
		
		// 合并班组对应的任务。
		List<TBzgzrw> bzrwList = planService
				.findListByHql("from TBzgzrw where planid in ("
						+ getHiddenids() + ")");
		String allrw = "";
		for (TPlanBz planbz : planbzList) {
			for (TBzgzrw rw : bzrwList) {
				if (planbz.getBzid().toString().equals(rw.getBzid().toString())) {
					planbz.setGzrw((planbz.getGzrw() == null ? "" : planbz.getGzrw()) + rw.getSbname() + rw.getGznr() + "\n");
					allrw += rw.getSbname() + rw.getGznr() + "\n";
				}
			}
		}
		// 工作项目
		entity.setWorkcontent("".equals(allrw) ? "" : allrw.substring(0, allrw.length() -2));
		return SUCCESS;
	}

	public List<TBdz> getBdzList() {
		return bdzList;
	}

	public List<TDictionary> getDydjList() {
		return dydjList;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public void setPlanService(PlanService planService) {
		this.planService = planService;
	}

	public TPlan getEntity() {
		return entity;
	}

	public void setEntity(TPlan entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<TPlanBz> getPlanbzList() {
		return planbzList;
	}

	public void setPlanbzList(List<TPlanBz> planbzList) {
		this.planbzList = planbzList;
	}

	public String getHiddenids() {
		if (this.hiddenids != null) {
			if (!StringUtils.isNullOrBlank(this.hiddenids)) {
				if (this.hiddenids.startsWith(",")) {
					this.hiddenids = this.hiddenids.substring(1, this.hiddenids
							.length());
				}
				if (this.hiddenids.endsWith(",")) {
					this.hiddenids = this.hiddenids.substring(0, this.hiddenids
							.length() - 1);
				}
			}
		}
		return this.hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getDatafrom() {
		return datafrom;
	}

	public void setDatafrom(String datafrom) {
		this.datafrom = datafrom;
	}

	public String getShowtype() {
		return showtype;
	}

	public void setShowtype(String showtype) {
		this.showtype = showtype;
	}

	public PlanService getPlanService() {
		return planService;
	}

	public void setBdzList(List<TBdz> bdzList) {
		this.bdzList = bdzList;
	}

	public void setDydjList(List<TDictionary> dydjList) {
		this.dydjList = dydjList;
	}

	public void setSblxList(List<TDictionary> sblxList) {
		this.sblxList = sblxList;
	}

}
